package com.cqjtu.eecs.service.impl;

import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.AssessScore;
import com.cqjtu.eecs.entity.AssessmentLink;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.Major;
import com.cqjtu.eecs.exception.CourseNotExistException;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.mapper.AssessmentLinkMapper;
import com.cqjtu.eecs.mapper.CourseMapper;
import com.cqjtu.eecs.mapper.MajorMapper;
import com.cqjtu.eecs.mapper.ProjectCourseMapper;
import com.cqjtu.eecs.service.IAssessScoreService;
import com.cqjtu.eecs.service.IAssessmentLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjtu.eecs.service.IProjectCourseService;
import com.cqjtu.eecs.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 考核环节 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-07
 */
@Service
public class AssessmentLinkServiceImpl extends ServiceImpl<AssessmentLinkMapper, AssessmentLink> implements IAssessmentLinkService {

    @Autowired
    private AssessmentLinkMapper assessmentLinkMapper;

    @Autowired
    private IProjectCourseService projectCourseService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IAssessScoreService assessScoreService;

    /**
     * 获取考核环节
     * @param grade 年级
     * @param majorName 专业名
     * @param courseName 课程名
     * @return
     */
    @Override
    public List<List<String>> getAssessmentLink(Integer grade, String majorName, String courseName) {
        // 获取方案-课程编号
        Integer id = projectCourseService.getProjectCourseId(grade, majorName, courseName);

        // 查询考核环节
        List<AssessmentLink> assessmentLinkList = assessmentLinkMapper.selectList(new QueryWrapper<AssessmentLink>().eq("project_course_id", id));
        System.out.println(assessmentLinkList);
        if (assessmentLinkList.size() == 0 || assessmentLinkList == null)
            throw new NotFoundException("未导入" + courseName + "的课程大纲");

        List<List<String>> headList = ListUtils.newArrayList();
        List<String> sid = ListUtils.newArrayList();
        sid.add("学号");
        headList.add(sid);
        List<String> name = ListUtils.newArrayList();
        name.add("姓名");
        headList.add(name);
        List<String> major = ListUtils.newArrayList();
        major.add("专业名称");
        headList.add(major);
        //考核环节excel head
        assessmentLinkList.forEach(item -> {
            List<String> head = ListUtils.newArrayList();
            head.add(item.getLinkName());
            headList.add(head);
        });

        List<String> score = ListUtils.newArrayList();
        score.add("总成绩");
        headList.add(score);
        return headList;
    }

    /**
     *  插入学生各个考核环节成绩
     *
     * @param grade 年级
     * @param courseName 课程名
     * @param tcId 教学班编号
     * @param dataList 学生成绩集合
     * @param headMap
     * @return
     */
    @Override
    public boolean insertStudentAssessmentLink(Integer grade, String courseName, Integer tcId, List<Map<Integer, String>> dataList, Map<Integer, String> headMap) {
        try {
            // 获取所有专业名
            Set<String> majorNames = new HashSet<>();
            dataList.forEach(item -> {
                majorNames.add(item.get(2));
            });

            // 获取该课程所有专业的考核环节名称编号映射
            Map<String, Map<String, Integer>> majorAssessmentLink = new HashMap<>();
            majorNames.forEach(majorName -> {
                Map<String, Integer> assessmentLinkMap = this.assessmentLinkMap(majorName, grade, courseName);
                majorAssessmentLink.put(majorName, assessmentLinkMap);
            });

            // 学生学号集合
            List<String> codeList = new ArrayList<>();
            dataList.forEach(item -> {
                codeList.add(item.get(0));
            });

            // 学号id映射
            Map<String, Integer> codeIdMap = studentService.getCodeIdMap(codeList);

            // 插入所有学生考核环节成绩
            List<AssessScore> assessScoreList = new ArrayList<>();
            int size = dataList.get(0).size();
            dataList.forEach(item -> {
                // 每条学生记录的各个考核环节成绩
                for (int i = 3; i < size - 1; i++) {
                    AssessScore assessScore = new AssessScore(tcId, codeIdMap.get(item.get(0)),
                            majorAssessmentLink.get(item.get(2)).get(headMap.get(i)), Float.parseFloat(item.get(i)));
                    assessScoreList.add(assessScore);
                }
            });
            boolean saveBatch = assessScoreService.saveBatch(assessScoreList);
            if (saveBatch)
                return true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取考核环节名称编号键值对
     * @param majorName 专业名
     * @param grade 年级
     * @param courseName 课程名
     * @return
     */
    private Map<String, Integer> assessmentLinkMap(String majorName, Integer grade, String courseName) {

        // 根据年级 专业代码 课程代码获取 方案-课程编号
        Integer pcId = projectCourseService.getProjectCourseId(grade, majorName, courseName);
        try {
            Map<String, Integer> nameIdMap = assessmentLinkMapper.selectList(new QueryWrapper<AssessmentLink>().eq("project_course_id", pcId))
                    .stream().collect(Collectors.toMap(AssessmentLink::getLinkName, AssessmentLink::getLinkId));
            return nameIdMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
